﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="Pages_Reservations_Ascx_ProgramPopup"
    CodeBehind="ProgramPopup.ascx.cs" %>
<%@ Register Src="~/Tools/NumericBox.ascx" TagName="NumericBox" TagPrefix="num" %>
<!-- Service Providers Dialog -->
<div id="program-dialog" title="Create service" style="display: none">
    <p class="validateTips">
        All form fields are required.</p>
    <fieldset>
        <table cellspacing="5">
            <tr>
                <td>
                    <asp:Label ID="lblServiceType" runat="server" AssociatedControlID="rdoServiceType">Type:</asp:Label>
                </td>
                <td colspan="3">
                    <asp:RadioButtonList ID="rdoServiceType" runat="server" RepeatDirection="Horizontal">
                    </asp:RadioButtonList>
                    <asp:HiddenField ID="hidIsEdit" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblDate" runat="server" AssociatedControlID="txtDate">Date:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
                <td>
                    <asp:Label ID="lblServiceDate" runat="server" AssociatedControlID="txtServiceDate">Service Date:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtServiceDate" runat="server" CssClass="datepicker"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblActivity" runat="server" AssociatedControlID="txtActivity">Activity:</asp:Label>
                </td>
                <td colspan="3">
                    <asp:TextBox ID="txtActivity" runat="server" Width="98%" CssClass="required"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblDescription" runat="server" AssociatedControlID="txtDescription">Description:</asp:Label>
                </td>
                <td colspan="3">
                    <asp:TextBox ID="txtDescription" runat="server" Width="98%" TextMode="MultiLine"
                        Rows="3" CssClass="required"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblItemCode" runat="server" AssociatedControlID="txtItemCode">Code:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtItemCode" runat="server" Width="98%" CssClass="required"></asp:TextBox>
                </td>
                <td>
                    <asp:Label ID="lblServiceStatus" runat="server" AssociatedControlID="ddlServiceStatus">Status:</asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlServiceStatus" Width="98%" runat="server">
                        <asp:ListItem Selected="True" Value="0">Pending</asp:ListItem>
                        <asp:ListItem Value="1">Confirmed</asp:ListItem>
                        <asp:ListItem Value="2">Cancelled</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr id="service">
                <td>
                    <asp:Label ID="lblServiceName" runat="server" AssociatedControlID="ddlServiceName">Service:</asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlServiceName" Width="98%" runat="server" AppendDataBoundItems="true">
                        <asp:ListItem Value="">Select a Service ...</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    <asp:Label ID="lblProvider" runat="server" AssociatedControlID="ddlProvider">Provider:</asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlProvider" Width="98%" runat="server">
                        <asp:ListItem Value="">Select a provider ...</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr id="hoteladdress">
                <td>
                    <asp:Label ID="lblHotelAddress" runat="server" AssociatedControlID="txtHotelAddress">Hotel Address:</asp:Label>
                </td>
                <td colspan="3">
                    <asp:TextBox ID="txtHotelAddress" runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox>
                </td>
            </tr>
            <tr id="qty">
                <td>
                    Quantity
                    <%--Number of&nbsp--%>
                    <asp:Label ID="lblUnitOfMeasure" runat="server" Visible="false" AssociatedControlID="txtPax"></asp:Label>:
                </td>
                <td>
                    <num:NumericBox ID="txtPax" runat="server" Width="98%" Text="0" />
                </td>
            </tr>
            <tr id="numofnight">
                <td>
                    <asp:Label ID="lblNights" runat="server" AssociatedControlID="txtNights">How many nights?</asp:Label>
                </td>
                <td>
                    <num:NumericBox ID="txtNights" runat="server" Width="98%" Text="0" />
                </td>
            </tr>
            <tr id="packagecost">
                <td>
                    <asp:Label ID="lblPackageCost" runat="server" AssociatedControlID="txtPackageCost">Package Cost:</asp:Label>
                </td>
                <td>
                    <num:NumericBox ID="txtPackageCost" Width="98%" runat="server"></num:NumericBox>
                </td>
            </tr>
            <tr id="unitcost">
                <td>
                    Expense<%-- per--%>
                    <asp:Label ID="lblUnitCost" runat="server" Visible="false" AssociatedControlID="txtUnitCost"></asp:Label>:
                </td>
                <td>
                    <num:NumericBox ID="txtUnitCost" Width="98%" runat="server" />
                </td>
                <td colspan="2">
                    <asp:DropDownList ID="ddlCurrencyType" Width="100px" runat="server">
                    </asp:DropDownList>
                    <asp:HiddenField ID="txtExchangeRate" runat="server" Value="1"></asp:HiddenField>
                </td>
            </tr>
            <tr id="totalcost">
                <td>
                    <asp:Label ID="lblServiceCost" runat="server" AssociatedControlID="txtAmount">Total Amount:</asp:Label>
                </td>
                <td>
                    <num:NumericBox ID="txtAmount" Width="98%" runat="server" Enabled="false" />
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <span id="litNoticePopup">
                        <asp:Literal ID="litNoticePopup1" runat="server" />
                    </span>
                </td>
            </tr>
        </table>
    </fieldset>
</div>
<script type="text/javascript">
    var hasShuttle = false;

    // Service dialog
    var rdoServiceTypeCtl = $("#<%=rdoServiceType.ClientID %>");
    var dateCtl = $("#<%=txtDate.ClientID %>");
    var serviceDateCtl = $("#<%=txtServiceDate.ClientID %>");

    $("tr#numofnight").css('display', 'none');
    $("tr#hoteladdress").css('display', 'none');

    $(document).ready(function () {

        rdoServiceTypeCtl.change(function () {
            $("#<%=ddlServiceName.ClientID %>").change();
        });

        $("#<%=ddlServiceName.ClientID %>").change(function () {
            var choiceOption = ($.trim($("#<%=ddlServiceName.ClientID %> option:selected").attr('ChoiceOption')) == '0');
            if (choiceOption) {
                $("tr#packagecost").css('display', '');
                $("#<%=txtPackageCost.ClientID %>").autoNumericSet($("#<%=ddlServiceName.ClientID %> option:selected").attr('PackagePrice'));
            }
            else {
                $("tr#packagecost").css('display', 'none');
                $("#<%=txtPackageCost.ClientID %>").val(0);
            }

            var unit = parseInt($("#<%=ddlServiceName.ClientID %> option:selected").attr('Unit'));
            var nightFlag = ($("#<%=ddlServiceName.ClientID %> option:selected").attr('NightFlag') == 'true');

            switch (unit) {
                case 0:
                    $("#<%=lblUnitOfMeasure.ClientID %>").html('pax');
                    $("#<%=lblUnitCost.ClientID %>").html('pax');
                    break;
                case 1:
                    $("#<%=lblUnitOfMeasure.ClientID %>").html('cabins');
                    $("#<%=lblUnitCost.ClientID %>").html('cabins');
                    break;
                case 2:
                    $("#<%=lblUnitOfMeasure.ClientID %>").html($("#<%=ddlServiceName.ClientID %> option:selected").attr('OtherUom'));
                    $("#<%=lblUnitCost.ClientID %>").html($("#<%=ddlServiceName.ClientID %> option:selected").attr('OtherUom'));
                    break;
            }
            if (nightFlag) $("tr#numofnight").css('display', ''); else $("tr#numofnight").css('display', 'none');

            var serviceID = $.trim($("#<%=ddlServiceName.ClientID %> option:selected").val());
            if (serviceID != '' && serviceID != null) {
                $("#<%=txtUnitCost.ClientID %>").autoNumericSet($("#<%=ddlServiceName.ClientID %> option:selected").attr('UnitPrice'));
            }

            $("#<%=ddlCurrencyType.ClientID %>").val($("#<%=ddlServiceName.ClientID %> option:selected").attr('Currency'));

            getTotalCost();

        });

        function getTotalCost() {
            var rate = getRate($("#<%=ddlCurrencyType.ClientID %> option:selected").text());

            if (rate != null) {
                $("#<%=txtExchangeRate.ClientID %>").val(rate.UnitPrice);

                var qty = parseInt($("#<%=txtPax.ClientID %>").val());
                var numInPackage = parseInt($("#<%=ddlServiceName.ClientID %> option:selected").attr('NumInPackage'));
                var nights = toFloat($("#<%=txtNights.ClientID %>").val());

                if (qty > 0) {
                    var unitCost = $("#<%=txtUnitCost.ClientID %>").autoNumericGet();
                    if (numInPackage > 0) {
                        // base on package
                        var amount = parseFloat($("#<%=txtPackageCost.ClientID %>").autoNumericGet()); // Package
                        if (qty > numInPackage) {
                            amount += (qty - numInPackage) * unitCost;
                        }
                    }
                    else {
                        amount = qty * unitCost;
                    }
                }
                if (nights > 0)
                    amount *= nights;

                amount *= rate.UnitPrice;
                $("#<%=txtAmount.ClientID %>").autoNumericSet(amount);
            }
        }

        $("#<%=txtPackageCost.ClientID %>").blur(function () { getTotalCost(); });
        $("#<%=txtUnitCost.ClientID %>").blur(function () { getTotalCost(); });
        $("#<%=ddlCurrencyType.ClientID %>").change(function () { getTotalCost(); });
        $("#<%=txtPax.ClientID %>").blur(function () { getTotalCost(); });
        $("#<%=txtNights.ClientID %>").blur(function () { getTotalCost(); });
    });

    $("#program-dialog").dialog({
        autoOpen: false,
        width: 539,
        modal: true,
        buttons: {
            "OK": function () {
                var e = "#<%=txtActivity.ClientID %>";
                var bValid = !($(e).val() == "" && $(e).hasClass('required'));

                if (bValid) {

                    var tbody = $('#gridProgram > tbody');
                    var checkbox = '<input type="checkbox" class="chkService" />';
                    var seq = ($('tr', tbody).length + 1);
                    var evenClass = (seq % 2 == 0 ? ' class="evenRow"' : '');
                    var date = Date.parse($("#<%=txtDate.ClientID %>").val());
                    var serviceDate = Date.parse($("#<%=txtServiceDate.ClientID %>").val());
                    var serviceID = $.trim($("#<%=ddlServiceName.ClientID %> option:selected").val());
                    var providerID = $.trim($("#<%=ddlProvider.ClientID %> option:selected").val());
                    var providerName = (providerID == '' ? '' : $("#<%=ddlProvider.ClientID %> option:selected").text());

                    var serviceName = '', otherUom = '', numInPackage = '0';

                    // Process unselect the service
                    if (serviceID != '') {
                        serviceName = $("#<%=ddlServiceName.ClientID %> option:selected").text();
                        otherUom = $("#<%=ddlServiceName.ClientID %> option:selected").attr('OtherUom');
                        numInPackage = $("#<%=ddlServiceName.ClientID %> option:selected").attr('NumInPackage');
                    }

                    try {
                        var isEdit = $.trim($("#<%=hidIsEdit.ClientID %>").val());
                        if (isEdit == '' || isEdit == '-1') {

                            var row = stringformat('<tr{0}><td>{1}</td>' + // CheckBox
                            '<td>{2}</td>' + // Date
                            '<td>{3}</td>' + // Activity
                            '<td>{4}</td>' + // Service (name)
                            '<td>{5}</td>' + // Provider (name)
                            '<td style="display: none">{6}</td>' + // Package Cost
                            '<td style="display: none">{7}</td>' + // Unit Cost
                            '<td>{8}</td>' + // Currency (name)
                            '<td style="display: none">{9}</td>' + // Rate
                            '<td style="text-align: right">{10}</td>' + // Amount
                            '<td>{11}</td>' + // Service Status
                            '<td>{12}</td>' + // Service Type
                            '<td style="display: none">{13}</td>' + // Pax
                            '<td style="display: none">{14}</td>' + // Nights
                            '<td style="display: none">{15}</td>' + // Other Uom
                            '<td style="display: none">{16}</td>' + // Num in Package
                            '<td style="display: none">{17}</td>' + // Service ID
                            '<td style="display: none">{18}</td>' + // Provider ID
                            '<td style="display: none">{19}</td>' + // Service Type ID
                            '<td style="display: none">{20}</td>' + // Status ID
                            '<td style="display: none">{21}</td>' + // Currency ID
                            '<td style="display: none">{22}</td>' + // Hotel Pickup
                            '<td style="display: none">{23}</td>' + // Description
                            '<td style="display: none">{24}</td>' + // ItemCode
                            '<td style="display: none">{25}</td>' + // Service Date
                            '<td><a class="btnEdit new">Edit</a>&nbsp;|&nbsp;<a class="btnDelete new">Remove</a></td></tr>',
                            evenClass,
                            checkbox,
                            dateFormat(date, "mm/dd/yyyy"),
                            $("#<%=txtActivity.ClientID %>").val(),
                            serviceName,
                            providerName,
                            $("#<%=txtPackageCost.ClientID %>").autoNumericGet(),
                            $("#<%=txtUnitCost.ClientID %>").autoNumericGet(),
                            $("#<%=ddlCurrencyType.ClientID %> option:selected").text(),
                            $("#<%=txtExchangeRate.ClientID %>").autoNumericGet(),
                            $("#<%=txtAmount.ClientID %>").autoNumericGet(),
                            $("#<%=ddlServiceStatus.ClientID %> option:selected").text(),
                            $("#<%=rdoServiceType.ClientID %> input:checked + label").text(),
                            $("#<%=txtPax.ClientID %>").autoNumericGet(),
                            $("#<%=txtNights.ClientID %>").autoNumericGet(),
                            otherUom,
                            numInPackage,
                            serviceID,
                            providerID,
                            $("#<%=rdoServiceType.ClientID %> input:checked").val(),
                            $("#<%=ddlServiceStatus.ClientID %> option:selected").val(),
                            $("#<%=ddlCurrencyType.ClientID %> option:selected").val(),
                            '',
                            $("#<%=txtDescription.ClientID %>").val(),
                            $("#<%=txtItemCode.ClientID %>").val(),
                            dateFormat(serviceDate, "mm/dd/yyyy"));

                            if ($.trim(tbody.html()) == '') {
                                tbody.html(row);
                            }
                            else {
                                var lastTR;

                                // Attach
                                $('tr', tbody).each(function () {
                                    if (Date.parse($(this).children("td:eq(1)").text()) <= date)
                                        lastTR = $(this);
                                });

                                if (lastTR != null)
                                    lastTR.after(row);
                                else
                                    $('tr:first', tbody).before(row);
                            }

                            rdoServiceTypeCtl.focus();
                            $("#<%=hidIsEdit.ClientID %>").val(-1);

                            $("#litNoticePopup").text("INSERT SUCCESSFUL.");

                            programEvents();
                        }
                        else {
                            var line = $("#gridProgram > tbody > tr:eq(" + $("#<%=hidIsEdit.ClientID %>").val() + ")");
                            $(line).children("td:eq(1)").text($("#<%=txtDate.ClientID %>").val()); // Date
                            $(line).children("td:eq(2)").text($("#<%=txtActivity.ClientID %>").val()); // Activity
                            if ($("#<%=ddlServiceName.ClientID %> option:selected").val() == '') // Service (name)
                                $(line).children("td:eq(3)").text('');
                            else
                                $(line).children("td:eq(3)").text($("#<%=ddlServiceName.ClientID %> option:selected").text());

                            if ($("#<%=ddlProvider.ClientID %> option:selected").val() == '') // Provider (name)
                                $(line).children("td:eq(4)").text('');
                            else
                                $(line).children("td:eq(4)").text($("#<%=ddlProvider.ClientID %> option:selected").text());

                            $(line).children("td:eq(5)").text($("#<%=txtPackageCost.ClientID %>").autoNumericGet()); // Package Cost
                            $(line).children("td:eq(6)").text($("#<%=txtUnitCost.ClientID %>").autoNumericGet()); // Unit Cost
                            $(line).children("td:eq(7)").text($("#<%=ddlCurrencyType.ClientID %> option:selected").text()); // Currency Name
                            $(line).children("td:eq(8)").text($("#<%=txtExchangeRate.ClientID %>").autoNumericGet()); // Rate
                            $(line).children("td:eq(9)").text($("#<%=txtAmount.ClientID %>").val()); // Amount = Package + (Number of pax * Unit cost)
                            $(line).children("td:eq(10)").text($("#<%=ddlServiceStatus.ClientID %> option:selected").text()); // Status (name)
                            $(line).children("td:eq(11)").text($("#<%=rdoServiceType.ClientID %> input:checked + label").text()); // Service type (name)
                            $(line).children("td:eq(12)").text($("#<%=txtPax.ClientID %>").autoNumericGet()); // Number of pax
                            $(line).children("td:eq(13)").text($("#<%=txtNights.ClientID %>").autoNumericGet()); // Number of night(s)
                            $(line).children("td:eq(14)").text(otherUom); // Other Uom
                            $(line).children("td:eq(15)").text(numInPackage); // Number in package
                            $(line).children("td:eq(16)").text(serviceID); // Service ID
                            $(line).children("td:eq(17)").text(providerID); // Provider ID
                            $(line).children("td:eq(18)").text($("#<%=rdoServiceType.ClientID %> input:checked").val()); // Service type ID
                            $(line).children("td:eq(19)").text($("#<%=ddlServiceStatus.ClientID %> option:selected").val()); // Status ID
                            $(line).children("td:eq(20)").text($("#<%=ddlCurrencyType.ClientID %> option:selected").val()); // Currency ID
                            $(line).children("td:eq(22)").text($("#<%=txtDescription.ClientID %>").val()); // Description
                            $(line).children("td:eq(23)").text($("#<%=txtItemCode.ClientID %>").val()); // ItemCode
                            $(line).children("td:eq(24)").text($("#<%=txtServiceDate.ClientID %>").val()); // Service Date

                            $("#<%=hidIsEdit.ClientID %>").val(-1);
                            $(this).dialog("close");
                        }
                    }
                    catch (e) {
                        $("#litNoticePopup").text(e + "INSERT FAILS.");
                    }
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            $("#<%=hidIsEdit.ClientID %>").val(-1);

            hasShuttle = false;
            hotelAddressCtl.val('');
            $("table#gridProgram > tbody > tr").each(function () {
                if (!hasShuttle && parseInt($(this).children("td:eq(18)").text()) == 2) {
                    hasShuttle = true;
                    hotelAddressCtl.val($(this).children("td:eq(21)").text());
                }
            });
        }
    });

    function programEvents() {
        // Edit
        $("#gridProgram a.btnEdit.new").click(function () {

            var idx = $("#gridProgram > tbody > tr").index($(this).closest('tr'));

            // Set values
            editProgram(idx);

            $("#program-dialog").show();
            $("#program-dialog").dialog("open");
        });

        $("#gridProgram a.btnEdit").removeClass("new");

        // Delete
        $("#gridProgram a.btnDelete.new").click(function () {
            if (confirm('Are you sure you want to delete this activity?'))
                $(this).closest('tr').remove();
        });

        $("#gridProgram a.btnDelete").removeClass("new");

        $("#gridProgram tbody tr").click(function () {
            $(this).addClass("highlight");
        });
    }

    function newProgram() {
        dateCtl.removeAttr('disabled');

        // clear
        $("#<%=txtDate.ClientID %>").val('');
        $("#<%=txtServiceDate.ClientID %>").val(dateFormat(new Date(), "mm/dd/yyyy"));
        $("#<%=txtActivity.ClientID %>").val('');
        $("#<%=txtPackageCost.ClientID %>").autoNumericSet(0);
        $("#<%=txtUnitCost.ClientID %>").autoNumericSet(0);
        $("#<%=txtExchangeRate.ClientID %>").autoNumericSet(0);
        $("#<%=txtAmount.ClientID %>").autoNumericSet(0);
        $("#<%=txtPax.ClientID %>").autoNumericSet(0);
        $("#<%=txtNights.ClientID %>").autoNumericSet(0);
        $("#<%=ddlServiceName.ClientID %>").val(null);
        $("#<%=ddlProvider.ClientID %>").val(null);
        $("#<%=ddlServiceStatus.ClientID %>").val(-1); // Pending ...
        $("#<%=ddlCurrencyType.ClientID %>").val(-1);
        $("#<%=txtHotelAddress.ClientID %>").val('');
        $("#<%=txtDescription.ClientID %>").val('');
        $("#<%=txtItemCode.ClientID %>").val('');
    }

    function editProgram(idx) {
        var line = $("#gridProgram > tbody > tr:eq(" + idx + ")");

        $("#<%=txtDate.ClientID %>").val($.trim($(line).children("td:eq(1)").text()));
        $("#<%=txtActivity.ClientID %>").val($.trim($(line).children("td:eq(2)").text()));
        $("#<%=txtPackageCost.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(5)").text()));
        $("#<%=txtUnitCost.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(6)").text()));
        $("#<%=txtExchangeRate.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(8)").text()));
        $("#<%=txtAmount.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(9)").text()));
        $("#<%=txtPax.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(12)").text()));
        $("#<%=txtNights.ClientID %>").autoNumericSet($.trim($(line).children("td:eq(13)").text()));
        $("#<%=rdoServiceType.ClientID %>").find("input[value='" + $.trim($(line).children("td:eq(18)").text()) + "']").attr("checked", "checked");
        $("#<%=ddlServiceStatus.ClientID %>").val(parseInt($.trim($(line).children("td:eq(19)").text()))); // Pending ...
        $("#<%=ddlCurrencyType.ClientID %>").val(parseInt($.trim($(line).children("td:eq(20)").text())));
        $("#<%=txtDescription.ClientID %>").val($.trim($(line).children("td:eq(22)").text()));
        $("#<%=txtItemCode.ClientID %>").val($.trim($(line).children("td:eq(23)").text()));

        var serviceID = $.trim($(line).children("td:eq(16)").text());
        var providerID = $.trim($(line).children("td:eq(17)").text());
        var serviceDate = $.trim($(line).children("td:eq(24)").text());

        if (serviceID != '' && serviceID != null) {
            $("#<%=ddlServiceName.ClientID %>").val(serviceID);
        }
        if (providerID != '' && providerID != null) {
            $("#<%=ddlProvider.ClientID %>").val(providerID);
        }
        if (serviceDate != '' && serviceDate != null) {
            $("#<%=txtServiceDate.ClientID %>").val(serviceDate);
        }
        else {
            $("#<%=txtServiceDate.ClientID %>").val(dateFormat(new Date(), 'mm/dd/yyyy'));
        }

        $("#<%=hidIsEdit.ClientID %>").val(idx);

        // Process Events
        $("#<%=rdoServiceType.ClientID %>").change();

        if ($.trim($(line).children("td:eq(18)").text()) == "1") {
            // Shared
            $("#<%=txtDate.ClientID %>").attr('disabled', true);
            $("#<%=rdoServiceType.ClientID %>").attr('disabled', true);
        }
        else {
            $("#<%=txtDate.ClientID %>").removeAttr('disabled');
            $("#<%=rdoServiceType.ClientID %>").removeAttr('disabled');
        }
    }

    function moveUp() {
        $("table#gridProgram tbody tr td input:checked").each(function () {
            var idx = $("table#gridProgram > tbody > tr").index($(this).closest('tr'));
            if (idx > 0)
                $('table#gridProgram tbody').moveRow(idx, idx - 1);
        });

    }

    function moveDown() {
        var length = $("table#gridProgram tbody tr").length - 1;
        $($("table#gridProgram tbody tr td input:checked").get().reverse()).each(function () {
            var idx = $("table#gridProgram > tbody > tr").index($(this).closest('tr'));
            if (idx < length)
                $('table#gridProgram tbody').moveRow(idx + 1, idx);
        });
    }

    function increaseDate() {
        if (confirm('Are you sure you want to increase date for the checked programs?')) {
            $("table#gridProgram tbody tr").each(function () {
                if ($(this).find("td:eq(0) input").is(":checked")) {
                    var dateOfRoute = Date.parse($(this).children("td:eq(1)").text());
                    var dt = dateOfRoute + 1000 * 60 * 60 * 24;
                    $(this).children("td:eq(1)").text(dateFormat(dt, "mm/dd/yyyy"));
                }
            });
        }
    }

    function getReservationServices(reservation) {
        reservation.ReservationServices = [];
        $("table#gridProgram > tbody > tr").each(function () {
            // Some info need to save.
            var program = {
                Date: $.trim($(this).children("td:eq(1)").text()),
                Program: $.trim($(this).children("td:eq(2)").text()),
                PackagePrice: toFloat($(this).children("td:eq(5)").text()),
                UnitPrice: toFloat($(this).children("td:eq(6)").text()),
                Rate: toFloat($(this).children("td:eq(8)").text()),
                Amount: toFloat($(this).children("td:eq(9)").text()),
                Pax: parseInt($(this).children("td:eq(12)").text()),
                Nights: parseInt($(this).children("td:eq(13)").text()),
                ServiceType: parseInt($(this).children("td:eq(18)").text()),
                Status: parseInt($(this).children("td:eq(19)").text()),
                Currency: parseInt($(this).children("td:eq(20)").text())
            };

            var serviceID = $.trim($(this).children("td:eq(16)").text());
            var providerID = $.trim($(this).children("td:eq(17)").text());
            var serviceDate = $.trim($(this).children("td:eq(24)").text());

            if (serviceID != null && serviceID != '') {
                program.ServiceID = serviceID;
                program.OtherUom = $.trim($(this).children("td:eq(14)").text());
                program.NumInPackage = parseInt($(this).children("td:eq(15)").text());
            }
            if (providerID != null && providerID != '') {
                program.ProviderID = providerID;
            }
            if (serviceDate != null && serviceDate != '') {
                program.ServiceDate = serviceDate;
            }

            reservation.ReservationServices.push(program);
        });
    }

</script>
